package com.nantai.jiafang.nantaijiafang.dialog;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import com.nantai.jiafang.nantaijiafang.R;

public class CustomAlertDialog extends Dialog {

	public CustomAlertDialog(Context context) {
		super(context);
	}

	public CustomAlertDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String message;
		private String title;
		private String btn_leftText;
		private String btn_rightText;
		private OnClickListener btn_leftClickListener;
		private OnClickListener btn_rightClickListener;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 * 
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 * 
		 * @param title
		 * @return
		 */

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 * 
		 * @param btn_leftText
		 * @return
		 */
		public Builder setbtn_left(int btn_leftText,
				OnClickListener listener) {
			this.btn_leftText = (String) context
					.getText(btn_leftText);
			this.btn_leftClickListener = listener;
			return this;
		}

		public Builder setbtn_left(String btn_leftText,
				OnClickListener listener) {
			this.btn_leftText = btn_leftText;
			this.btn_leftClickListener = listener;
			return this;
		}

		public Builder setbtn_right(int btn_rightText,
				OnClickListener listener) {
			this.btn_rightText = (String) context
					.getText(btn_rightText);
			this.btn_rightClickListener = listener;
			return this;
		}

		public Builder setbtn_right(String btn_rightText,
				OnClickListener listener) {
			this.btn_rightText = btn_rightText;
			this.btn_rightClickListener = listener;
			return this;
		}

		public CustomAlertDialog create() {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final CustomAlertDialog dialog = new CustomAlertDialog(context, R.style.Dialog_load);
			dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
			View layout = inflater.inflate(R.layout.common_alert_dialog, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title
//			((TextView) layout.findViewById(R.id.title)).setText(title);
			// set the confirm button
			if (btn_leftText != null) {
				((Button) layout.findViewById(R.id.btn_left))
						.setText(btn_leftText);
				if (btn_leftClickListener != null) {
					((Button) layout.findViewById(R.id.btn_left))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									btn_leftClickListener.onClick(dialog,
											DialogInterface.BUTTON_POSITIVE);
								}
							});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.btn_left).setVisibility(
						View.GONE);
//				layout.findViewById(R.id.line).setVisibility(
//                        View.GONE);
			}
			// set the btn_left button
			if (btn_rightText != null) {
//				((TextView) layout.findViewById(R.id.btn_right))
//						.setText(btn_rightText);
//				if (btn_rightClickListener != null) {
//					((TextView) layout.findViewById(R.id.btn_right))
//							.setOnClickListener(new View.OnClickListener() {
//								public void onClick(View v) {
//									btn_rightClickListener.onClick(dialog,
//											DialogInterface.BUTTON_NEGATIVE);
//								}
//							});
//				}
			} else {
				// if no confirm button just set the visibility to GONE
//				layout.findViewById(R.id.btn_right).setVisibility(
//						View.GONE);
			}
			// set the content message
			((TextView) layout.findViewById(R.id.common_alert_msg)).setText(message);
			
			dialog.setContentView(layout);
			return dialog;
		}

	}
}
